﻿using IMark.DAO;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IMark.Admin.Account
{
    public partial class Add : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Utils.CheckAuthentication("SAdmin"))
            {
                if (!IsPostBack)
                {
                    LoadData();
                }
            }
            else
            {
                Response.Redirect("~/Admin/Account/Home.aspx");
            }
        }
        private void LoadData()
        {
            info.InnerHtml = string.Empty;
            if ((string)Session["Info"] != "")
            {
                info.InnerHtml = (string)Session["Info"];
                Session["Info"] = "";
            }
            LoadGender();
            //LoadRole();
        }
        private void LoadGender()
        {
            StringBuilder str = new StringBuilder();
            str.Append("<select class=\"form-control\" id=\"gender\" name=\"gender\">");
            str.Append("<option value=\"0\">Nam</option>");
            str.Append("<option value=\"1\">Nữ</option>");
            str.Append("</select>");
            lstGender.InnerHtml = str.ToString();
        }
        //private void LoadRole()
        //{
        //    StringBuilder str = new StringBuilder();
        //    str.Append("<select class=\"form-control\" id=\"role\" name=\"role\">");
        //    str.Append("<option value=\"2\">Admin</option>");
        //    str.Append("<option value=\"1\">Super Admin</option>");
        //    str.Append("</select>");
        //    lstRole.InnerHtml = str.ToString();
        //}

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            NGUOI_DUNG nguoiDung = new NGUOI_DUNG();
            nguoiDung.TenDangNhap = txtUserName.Value;
            nguoiDung.MatKhau = Utils.ConvertMD5(txtPassword.Value);
            nguoiDung.DiaChi = txtAddress.Value;
            nguoiDung.Email = txtEmail.Value;
            nguoiDung.HoTen = txtFullName.Value;
            nguoiDung.GioiTinh = int.Parse(Request.Form["gender"]);
            nguoiDung.IDLoaiNguoiDung = 2;// int.Parse(Request.Form["role"]);
            nguoiDung.DienThoai = txtPhone.Value;
            nguoiDung.NgaySinh = DateTime.ParseExact(txtBirthDay.Value, "dd/MM/yyyy", new CultureInfo("en-US"));
            bool status = NguoiDungModels.Add(nguoiDung);
            if (status)
            {
                Session["Info"] = "<div class='alert alert-success'>Thêm tài khoản thành công !!!</div>";
                Response.Redirect("~/Admin/Account/Index.aspx");
            }
            else
            {
                info.InnerHtml = "<div class='alert alert-danger'>Đã xảy ra lỗi, vui lòng thử lại !!!</div>";
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Admin/Account/Home.aspx");
        }
        
    }
}